#
# pH+ Collection
#

module Ph

  class Collection < Hash
  
    def initialize
      super
      @index = 0
    end
        
    #
    # Insert Object, Use the name of the object as its key. If there is another hash it
    # merges the content of both.
    #
    def add (*objects)
      for i in objects
        case i.class.name 
        when 'Ph::Collection'
          self.merge!(i)
        when 'Hash'
					i.each { |k, v|
						self[k] = v
						v.id= k.to_s if (v.respond_to? :id)
					}
				else
          self[@index += 1] = i  
        end
        
      end
      return self
  end
  
  def remove(*objects)
    for i in objects
      self.delete i
    end
  end
    
=begin    
    # Adds Object to collection by key
    def addAs (key, object)
      if @data.has_key?(key) then puts 'WARNING overriding key ' + key.to_s end
      @data[key] = object
      return self
    end
        
    #
    # Automatic Add
    #
    def << (what)
      if (what.respond_to?(:id)) then
        self.addAs(:id, what)
      else
        self.add(what)
      end
      
      return self
    end
=end 
        
    def to_s
      result = String.new
      
      self.each { |key, value|
        result = result + value.to_s
      }

      return result
    end  
  
  end
  
end
